<%@ page import="org.springframework.web.context.request.SessionScope" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%request.setAttribute("ctx", request.getContextPath());%>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui-v2.9.14/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/jquery-2.1.0.min.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui_ext/xmSelect/xm-select.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/crypto-js.js"></script>
    <title>用户列表</title>
</head>
<body>
<form class="layui-form layui-row layui-col-space16">
    <div class="layui-col-md4">
        <div class="layui-input-wrap">
            <div class="layui-input-prefix">
                <i class="layui-icon layui-icon-username"></i>
            </div>
            <input type="text" name="A" value="" placeholder="Field A" class="layui-input" lay-affix="clear">
        </div>
    </div>
    <div class="layui-col-md4">
        <div class="layui-input-wrap">
            <input type="text" name="B" placeholder="Field B" lay-affix="clear" class="layui-input">
        </div>
    </div>
    <div class="layui-col-md4">
        <div class="layui-input-wrap">
            <div class="layui-input-prefix">
                <i class="layui-icon layui-icon-date"></i>
            </div>
            <input type="text" name="C" readonly placeholder="Field C" class="layui-input demo-table-search-date">
        </div>
    </div>
    <div class="layui-btn-container layui-col-xs12">
        <button class="layui-btn" lay-submit lay-filter="demo-table-search">Search</button>
        <button type="reset" class="layui-btn layui-btn-primary">Clear</button>
    </div>
</form>
<button class="layui-btn" lay-on="e1">添加人员</button>
<button class="layui-btn" lay-on="e2">导出所有</button>
<button class="layui-btn" lay-on="e3">导出所选的</button>
<button type="button" class="layui-btn" id="ID-upload-demo-btn">
    <i class="layui-icon layui-icon-upload"></i> 导入
</button>
<table class="layui-hide" id="ID-table-demo-search"></table>

<script>
    layui.use(function(){
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var util = layui.util;
        var upload = layui.upload;
        var layer = layui.layer;
        var element = layui.element;
        var $ = layui.$;
        // 单图片上传
        var uploadInst = upload.render({
            elem: '#ID-upload-demo-btn',
            url: '/user/importExcel.do', // 实际使用时改成您自己的上传接口即可。
            accept: 'file',
            exts: 'xls|xlss',

            done: function(res){
                // 若上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                // 上传成功的一些操作
                // …
                alert("山传成功")
                table.render("ID-table-demo-search")
                $('#ID-upload-demo-text').html(''); // 置空上传失败的状态
            },


        });
        //  2.9+ 版本可省略 attr 参数，默认读取 lay-on
        util.on({
            e1: function(){
                location.href="/user/toadd.do"
            },
            e2: function(){
                location.href="/user/exportExcel.do"
            },
            e3: function(){
                var checkStatus = table.checkStatus("ID-table-demo-search")
                var data = checkStatus.data; // 获取选中的数据
                ids = data.map(function (item) {
                    return item.userId
                }).join(",");

                location.href="/user/exportExcel2.do?ids="+ids;

            },
        });
        // 创建表格实例
        table.render({
            elem: '#ID-table-demo-search',
            url: '/user/list.do', // 此处为静态模拟数据，实际使用时需换成真实接口
            parseData:function (res) {
                return{
                    code:res.code,
                    data:res.data.list,
                    count:res.data.total,
                }
            },
            limit:3,
            cols: [[
                {type: 'checkbox', title: '😊', fixed: true}, // 单选框
                {field:'userId', title: '序号', width:80, sort: true, fixed: true},
                {field:'username', title: '人员姓名', width:150},
                {field:'roleName', title: '所属角色', width:150, sort: true},
                {field:'phone', title: '手机号', width:180},
                {field:'email', title: '邮箱', width:180},
                {field:'lastLoginTime', title: '最近登录时间', width:180},
            ]],
            page: true,
            height: 310
        });
        // 日期
        laydate.render({
            elem: '.demo-table-search-date'
        });
        // 搜索提交
        form.on('submit(demo-table-search)', function(data){
            var field = data.field; // 获得表单字段
            // 执行搜索重载
            table.reload('ID-table-demo-search', {
                page: {
                    curr: 1 // 重新从第 1 页开始
                },
                where: field // 搜索的字段
            });
            layer.msg('搜索成功<br>此处为静态模拟数据，实际使用时换成真实接口即可');
            return false; // 阻止默认 form 跳转
        });
    });
</script>

</body>
</html>